package com.jgsu.farm.controller;

import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.Image;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.ImageService;
import com.jgsu.farm.utils.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @program: Farm
 * @description: 关于图片的修改的功能
 * @author: Mr.zuo
 * @create: 2019-10-21 15:24
 **/
@Slf4j
@RestController
@RequestMapping("farm/sysuser/image")
public class ImageController {

    @Autowired
    ImageService imageService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    /**
     * 修改图片
     * path：/updateimage
     */
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult imageUpdate(Integer imageid,String name, String format, String description, String author,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time, String location,
                                    String device, MultipartFile imagefile) throws IOException {
        String imageFile = fileUploadUtil.upload(imagefile);
        Image image = new Image(imageid,name, format, description, author, time, location, device, imageFile);
        try {
            imageService.updateImage(image);
            log.info("图片资源更新成功，name为：{}", image.getName());
            return CommonResult.success(image);
        } catch (DataNotExistException e) {
            log.error("图片资源更新失败,name为：{}", image.getName());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     *删除图片
     * */
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult imageDelete(Integer imageid) {
        try {
            imageService.deleteImage(imageid);
            log.info("删除图片资源id为{}成功",imageid);
            log.info("删除match_image表的id为{}成功",imageid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("图片资源表中没有id{},删除失败",imageid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 添加图片
     */
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult imageAdd(@RequestParam(required = false, value = "name") String name,
                                 @RequestParam(required = false, value = "format") String format,
                                 @RequestParam(required = false, value = "description") String description,
                                 @RequestParam(required = false, value = "author") String author,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                 @RequestParam(required = false, value = "location") String location,
                                 @RequestParam(required = false, value = "device") String device,
                                 @RequestParam(required = false, value = "imagefile") MultipartFile imagefile) throws IOException {

        String imageFile = fileUploadUtil.upload(imagefile);
        Image image = new Image(name, format, description, author, time, location, device, imageFile);
        try {
            imageService.addImage(image);
            log.info("图片资源添加成功，name为：{}", image.getName());
            return CommonResult.success(image);
        } catch (DataAddException e) {
            log.error("图片资源添加异常,name为：{}", image.getName());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }


    /**
     * 分页查询图片信息
     */

    @ApiOperation(value = "图片资源查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Image>> imageList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                   @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Image> imageList = imageService.selectAll(page, pagesize);
            log.info("查询所有的图片资源");
            return CommonResult.success(imageList);
        } catch (DataNotExistException e) {
            log.error("图片资源表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
